package com.dungnt.vtime.event;

import com.dungnt.model.DbHelper;
import com.dungnt.vtime.common.cls.VLog;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.CalendarContract.EventDays;

public class EventProvider extends ContentProvider {
	// for database use
	private SQLiteDatabase eventsDB;
	private static final String DATABASE_NAME = "Events";
	private static final String DATABASE_TABLE = "Events";
	private static final int DATABASE_VERSION = 1;

	public static final String EVENT_PROVIDER_NAME = "com.dungnt.vtime.event.EventProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ EVENT_PROVIDER_NAME + "/" + DATABASE_NAME);
	public static final int EVENTS = 1;
	// Columns name
	public static final String _ID = "id";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String TIME = "time";
	public static final String FOLDER = "folder";
	public static final String IMAGE = "img";
	public static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(EVENT_PROVIDER_NAME, "Events", EVENTS);
	}

	private static final String DATABASE_CREATE = "CREATE TABLE "
			+ DATABASE_TABLE + " (" + _ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE
			+ " TEXT NOT NULL, " + CONTENT + " TEXT NOT NULL, " + FOLDER
			+ " INTEGER, " + TIME + " LONG, " + IMAGE + " BLOB);";

	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			VLog.D(DATABASE_CREATE);
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			VLog.D(DATABASE_CREATE);
			onCreate(db);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case EVENTS:
			count = eventsDB.delete(DATABASE_TABLE, selection, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		long rowID = eventsDB.insert(DATABASE_TABLE, "", values);
		if (rowID > 0) {
			Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(uri, null);
			return _uri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		Context context = getContext();
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		eventsDB = dbHelper.getWritableDatabase();
		return (eventsDB == null) ? false : true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
		sqlBuilder.setTables(DATABASE_TABLE);
		Cursor c = sqlBuilder.query(eventsDB, projection, selection,
				selectionArgs, null, null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case EVENTS:
			count = eventsDB.update(DATABASE_TABLE, values, selection,
					selectionArgs);
			break;

		default:
			break;
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
